package com.jinbei.common.socket;


import com.jinbei.common.base.SocketBase;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.websocket.OnClose;
import javax.websocket.OnError;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.concurrent.CopyOnWriteArraySet;

/**
 * @author zflu
 * 该类为提供外部 socket链接访问
 * Created by lucky on 2018/7/31.
 */
@ServerEndpoint("/messageSocket")
@Component
public class MessageWebSocket {

    private final Logger logger = LoggerFactory.getLogger(MessageWebSocket.class);

    /**
     * 记录连接数
     */
    private static int onlineCount = 0;

    /**
     * 回话对象
     */
    private static CopyOnWriteArraySet<MessageWebSocket> webSocketSet = new CopyOnWriteArraySet<>();

    /**
     * session对象
     */
    private Session session;


    /**
     * @author zflu
     * @Description   打开链接
     * @date 2018/7/31 19:03
     * @param session
     * @return void
     */
    @OnOpen
    public void onOpen(Session session) {
        this.session = session;
        webSocketSet.add(this);
        //连接数量++
        addOnline();
        try {
            logger.info("有客户端连接成功..... ");
            String s = SocketBase.successWebSocket("连接成功......", "system");
            //发送消息给客户端
            sendMessage(s);
        }catch (Exception e){
            logger.error("客户端连接失败..... ",e.getMessage());
        }
    }

    /**
     * @author zflu
     * @Description 关闭链接
     * @date 2018/7/31 19:03
     * @param
     * @return void
     */
    @OnClose
    public void onClose() {
        webSocketSet.remove(this);
        //连接数--
        closeOnline();
        logger.info("有客户端链接断开...... ");
    }

    /**
     * @author zflu
     * @Description   异常错误
     * @date 2018/7/31 19:35
     * @param session, error
     * @return void
     */
    @OnError
    public void onError(Session session, Throwable error) {
        try {
            logger.error("web socket error...... ,内容是: ",error);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * @author zflu
     * @Description  主动推送消息
     * @date 2018/7/31 19:06
     * @param message
     * @return void
     */
    public void sendMessage(String message) throws IOException {
        this.session.getBasicRemote().sendText(message);
    }

    /**
     * @author zflu
     * @Description   推送消息给所有的连接socket客户端
     * @date 2018/7/31 19:32
     * @param message
     * @return void
     */
    public void sendAllSocketMessage(String message) throws IOException {
        for (MessageWebSocket info:webSocketSet) {
            info.sendMessage(message);
        }
    }

    /**
     * @author zflu
     * @Description   连接数 ++
     * @date 2018/7/31 19:24
     * @param
     * @return void
     */
    public synchronized void addOnline(){
        MessageWebSocket.onlineCount ++;
    }

    /**
     * @author zflu
     * @Description   连接数--
     * @date 2018/7/31 19:25
     * @param
     * @return void
     */
    public synchronized void closeOnline(){
        MessageWebSocket.onlineCount --;
    }

    /**
     * @author zflu
     * @Description   统计连接数
     * @date 2018/7/31 19:26
     * @param
     * @return int
     */
    public synchronized int countOnline(){
        return MessageWebSocket.onlineCount;
    }


}
